package com.wmd.core.service.impl;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wmd.core.mybatis.union.UnionSelectDao;
import com.wmd.core.operation.UnionCriteria;
import com.wmd.core.service.UnionService;
import com.wmd.core.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Simple to Introduction
 *
 * @projectName:core-platform
 * @packageName:com.wmd.core.service.impl
 * @className:UnionServiceImpl
 * @Description:[一句话描述该类的功能]
 * @Author:luyincheng
 * @CreateDate:2021/1/20 17:26
 * @blog:[方法具体上行/下行参数文档]
 */
@Service
public class UnionServiceImpl implements UnionService {
    private static final Logger log = LoggerFactory.getLogger(UnionServiceImpl.class);

    @Resource
    private DynamicRoutingDataSource routingDataSource;

    @Autowired
    private UnionSelectDao unionSelectDao;
    @Override
    public IPage<Map<String, Object>> selectByUnionCriteria(Page<?> page, UnionCriteria unionCriteria, boolean isSQL) {

        if(StringUtil.isEmpty(unionCriteria.getResultColumn())){
            unionCriteria.setResultColumn(unionCriteria.getFromClause().calculateResultColuse());
        }

        if(isSQL){
            String sql=unionCriteria.GeneratorSql();
            log.info(sql);
            return this.unionSelectDao.selectBySql(page,sql);
        }else {
            return this.unionSelectDao.selectByExample(page,unionCriteria);
        }
    }

    @Override
    public List<Map<String, Object>> selectByUnionCriteriaNonePage(UnionCriteria unionCriteria, boolean isSQL) {
        if(StringUtil.isEmpty(unionCriteria.getResultColumn())){
            unionCriteria.setResultColumn(unionCriteria.getFromClause().calculateResultColuse());
        }

        if(isSQL){
            String sql=unionCriteria.GeneratorSql();
            log.info(sql);
            return this.unionSelectDao.selectBySqlNonePage(sql);
        }else {
            return this.unionSelectDao.selectByExampleNonePage(unionCriteria);
        }
    }



    @Override
    public IPage<Map<String, Object>> selectByUnionCriteria(String DS, Page<?> page, UnionCriteria unionCriteria, boolean isSQL) {
        IPage<Map<String, Object>> data=null;
        if(listDataSource().stream().anyMatch(
                        dataSourceId -> dataSourceId.equalsIgnoreCase(DS))){
            DynamicDataSourceContextHolder.push(DS);
            data=selectByUnionCriteria(page,unionCriteria,isSQL);
            DynamicDataSourceContextHolder.poll();
        }else {
            data=selectByUnionCriteria(page,unionCriteria,isSQL);
        }
        return data;
    }

    @Override
    public List<Map<String, Object>> selectByUnionCriteriaNonePage(String DS, UnionCriteria unionCriteria, boolean isSQL) {
        List<Map<String, Object>> data=null;
        if(listDataSource().stream().anyMatch(
                dataSourceId -> dataSourceId.equalsIgnoreCase(DS))){
            DynamicDataSourceContextHolder.push(DS);
            data=selectByUnionCriteriaNonePage(unionCriteria,isSQL);
            DynamicDataSourceContextHolder.poll();
        }else {
            data=selectByUnionCriteriaNonePage(unionCriteria,isSQL);
        }
        return data;
    }

    @Override
    public List<String> listDataSource() {
        return new ArrayList<>(routingDataSource.getDataSources().keySet());
    }
}
